<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./stylesheets/reset.css">
  <style>
    .chat{
      height: 600px;
      width: 300px;
      border: 4px solid #999;
      border-radius: 10px;
      margin: auto;
    }
    .title{
      line-height: 50px;
      text-align: center;
      background-color: #F7F7F7;
    }
    .msg-list{
      height: 500px;
      overflow-y: scroll;
      /* border: 1px solid red; */
    }
    .send-cont{
      height: 50px;
      border: 1px solid #666;
      display: flex;
      align-items: center;
    }
    .send-cont img{
      height: 50px;
      width: 50px;
      border-radius: 50%;
    }
    .send-cont input{
      height: 50px;
      border: none;
      padding: 0;
      /* 让当前元素占据Flex容器的剩余所有空间 */
      flex: 1;
    }
    .send-cont .btn{
      height: 50px;
      width: 60px;
      background-color: #7EA1FD;
      color: #fff;
      line-height: 50px;
      text-align: center;
    }
    .msg{
      /* border: 1px solid blue; */
      display: flex;
      padding: 10px;
    }
    .msg img{
      width: 40px;
      height: 40px;
      border-radius: 50%;
    }
    .msg p{
      background-color: #fff;
      box-shadow: 2px 2px 5px 2px #7FA0FD;
      line-height: 40px;
      padding: 0 10px;
      margin-left: 10px;
      word-break: break-all;
      border-radius: 10px;
      border-top-left-radius: 0;
    }
    .msg.self{
      justify-content:end;
      flex-direction: row-reverse;
    }
    .msg.self p{
      margin-right: 10px;
      margin-left: 0;
      background-color: #7FA0FD;
      color: #fff;
      border-radius: 10px;
      border-top-right-radius: 0;
    }
  </style>
</head>
<body>
  <div class="chat">
    <div class="title">聊天</div>
    <div class="msg-list" id="msg-list">
      <div class="msg">
        <img src="./img/girl.jpg" alt="" srcset="">
        <p>这是消息asdfasdfasdfasdfasdfasdfasdfasdf你好呵呵哒</p>
      </div>
      <div class="msg self">
        <img src="./img/boy.jpg" alt="" srcset="">
        <p>这是消息</p>
      </div>
    </div>
    <div class="send-cont">
      <img id="avatar" src="./img/girl.jpg" alt="" srcset="">
      <input type="text" id="msg">
      <div class="btn" id="send">发送</div>
    </div>
  </div>
</body>
<script>
  //切头像
  var oAvatar = document.getElementById('avatar')
  var state = true  //控制图片切换,true对应 girl
  oAvatar.onclick = function(){
    if(state){
      oAvatar.src = './img/boy.jpg'
    }else{
      oAvatar.src = './img/girl.jpg'
    }  
    state = !state  //赋值运算
  }

  // 发消息
  var oSend = document.getElementById('send')
  var oMsg = document.getElementById('msg')
  var oMsgList = document.getElementById('msg-list')
  oSend.onclick = function(){
    var msg = oMsg.value
    socket.send(msg)  //6. 客户端向服务端发消息
    oMsg.value = ''  
  }

  // 建立socket连接
  // 创建一个 WebSocket 连接
  // const socket = new WebSocket("ws://localhost:8080"); //2. 尝试在客户端连接socket服务
  const socket = new WebSocket("ws://1.95.67.194:8080"); //2. 尝试在客户端连接socket服务
  // 5. 监听服务端下发的消息（初始欢迎）  9. 别人发送消息
  socket.addEventListener("message",async function (event) {
    let data = await event.data
    console.log("Message from server ", data);
    var self = state ? '' : 'self' 
    var avatar = state ? 'girl' : 'boy' 
    oMsgList.innerHTML +=  `<div class="msg ${self}">
                              <img src="./img/${avatar}.jpg" alt="" srcset="">
                              <p>${data}</p>
                            </div>`  
  });

</script>
</html>